package org.jeecg.modules.demo.jxcx.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.jeecg.modules.demo.ddyht.controller.DdygPiciController;
import org.jeecg.modules.demo.jxcx.Utils.R;
import org.jeecg.modules.demo.jxcx.Utils.RandomUtil;
import org.jeecg.modules.demo.jxcx.dto.paymethod;
import org.jeecg.modules.demo.jxcx.po.*;
import org.jeecg.modules.demo.jxcx.service.JxcCustomService;
import org.jeecg.modules.demo.jxcx.service.JxcCzrzService;
import org.jeecg.modules.demo.jxcx.service.JxcPayinforService;
import org.jeecg.modules.demo.jxcx.service.JxcSupplierService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@RestController
@RequestMapping("jxcPayinfor")
public class JxcPayinforController {
    private static Logger logger = LoggerFactory.getLogger(JxcPayinforController.class);
    @Resource
    private JxcPayinforService jxcPayinforService;
    @Resource
    private JxcSupplierService jxcSupplierService;
    @Resource
    private JxcCustomService jxcCustomService;
    @Resource
    private JxcCzrzService jxcCzrzService;

    @PostMapping("/queryallpays")
    @ResponseBody
    public R queryallpays(@RequestBody JSONObject params) {
        String companyId = params.getString("companyId");
        List<paymethod> getpaymethodall = jxcPayinforService.getpaymethodall(companyId);
        return new R().ok(getpaymethodall);
    }
    @PostMapping("/queryall")
    @ResponseBody
    public R queryall(@RequestBody JSONObject params) {
        String companyId = params.getString("companyId");
        String orstate = params.getString("orstate");
        String traner = params.getString("traner");
        return new R().ok(jxcPayinforService.getByall(companyId,traner, orstate));
    }
    /**
     * 新增数据
     *
     * @param jxcPayinfor 实体对象
     * @return 新增结果
     */
    @PostMapping("add")
    public R insert(@RequestBody JxcPayinfor jxcPayinfor) {
        LocalDateTime now = LocalDateTime.now();
        // 定义日期时间格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
        DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        String formattedDateTime = now.format(formatter);
        String only1 = RandomUtil.generateRandomString();
        String id = now.format(formatter2) + only1;

        jxcPayinfor.setId(id);
        jxcPayinfor.setPaytime(formattedDateTime);

        String orstate = jxcPayinfor.getOrstate();
        String traner = jxcPayinfor.getOther().trim();
        String CompanyId = jxcPayinfor.getOtherid();
        BigDecimal payamount = new BigDecimal(jxcPayinfor.getPayamount());
        if (orstate.equals("1")){
            //供应商
            JxcSupplier jxcSupplier = jxcSupplierService.getBy(traner, CompanyId);
            if (jxcSupplier != null){
                BigDecimal oldSupplieramount = new BigDecimal(jxcSupplier.getAmount());
                jxcSupplier.setAmount(String.valueOf(oldSupplieramount.subtract(payamount)));
                jxcSupplierService.updateById(jxcSupplier);

                JxcCzrz jxcCzrz1 = new JxcCzrz();
                jxcCzrz1.setId(DdygPiciController.setczrzid(id));
                jxcCzrz1.setCompanyId(CompanyId);
                jxcCzrz1.setInfor("交易方:"+traner+"金额-"+payamount);
                jxcCzrz1.setType("供应商修改");
                R add1 = jxcCzrzService.add(jxcCzrz1);
            }
        }else {
            JxcCustom jxcCustom = jxcCustomService.getBy(traner, CompanyId);
            if (jxcCustom != null){
                BigDecimal oldCustomamount = new BigDecimal(jxcCustom.getAmount());
                jxcCustom.setAmount(String.valueOf(oldCustomamount.subtract(payamount)));
                jxcCustomService.updateById(jxcCustom);

                JxcCzrz jxcCzrz1 = new JxcCzrz();
                jxcCzrz1.setId(DdygPiciController.setczrzid(id));
                jxcCzrz1.setCompanyId(CompanyId);
                jxcCzrz1.setInfor("交易方:"+traner+"金额-"+payamount);
                jxcCzrz1.setType("客户修改");
                R add1 = jxcCzrzService.add(jxcCzrz1);
            }

        }
        JxcCzrz jxcCzrz = new JxcCzrz();
        jxcCzrz.setId(DdygPiciController.setczrzid(id));
        jxcCzrz.setCompanyId(CompanyId);
        jxcCzrz.setInfor("交易方:"+traner+"金额:"+payamount);
        jxcCzrz.setType("付款明细新增");
        R add = jxcCzrzService.add(jxcCzrz);


        return new R().ok(jxcPayinforService.save(jxcPayinfor));
    }
    /**
     * 修改数据
     *
     * @param jxcPayinfor 实体对象
     * @return 修改结果
     */
    @PutMapping("update")
    public R update(@RequestBody JxcPayinfor jxcPayinfor) {
        String orstate = jxcPayinfor.getOrstate();
        String companyId = jxcPayinfor.getOtherid();
        BigDecimal payamount = new BigDecimal(jxcPayinfor.getPayamount());
        String traner = jxcPayinfor.getOther().trim();
        String id = jxcPayinfor.getId();

        //旧数据
        JxcPayinfor oldJxcPayinfor = jxcPayinforService.getById(jxcPayinfor.getId());
        BigDecimal oldpayamount = new BigDecimal(oldJxcPayinfor.getPayamount());

        JxcCzrz jxcCzrz = new JxcCzrz();
        jxcCzrz.setId(DdygPiciController.setczrzid(id));
        jxcCzrz.setCompanyId(companyId);
        jxcCzrz.setInfor("交易方:"+traner+"金额:"+oldpayamount + "->"+payamount);
        jxcCzrz.setType("付款明细修改");
        R add = jxcCzrzService.add(jxcCzrz);
        //如果比原来多
        if (payamount.compareTo(oldpayamount) > 0){
            //差值
            BigDecimal diff = payamount.subtract(oldpayamount);
            if (orstate.equals("1")){
                //供应商
                JxcSupplier jxcSupplier = jxcSupplierService.getBy(traner, companyId);
                if (jxcSupplier != null){
                    BigDecimal oldSupplieramount = new BigDecimal(jxcSupplier.getAmount());
                    jxcSupplier.setAmount(String.valueOf(oldSupplieramount.subtract(diff)));
                   jxcSupplierService.updateById(jxcSupplier);

                    JxcCzrz jxcCzrz1 = new JxcCzrz();
                    jxcCzrz1.setId(DdygPiciController.setczrzid(id));
                    jxcCzrz1.setCompanyId(companyId);
                    jxcCzrz1.setInfor("交易方:"+traner+"金额+"+String.valueOf(diff));
                    jxcCzrz1.setType("供应商修改");
                    R add1 = jxcCzrzService.add(jxcCzrz1);
                }
            }else {
                JxcCustom jxcCustom = jxcCustomService.getBy(traner, companyId);
                if (jxcCustom != null){
                    BigDecimal oldCustomamount = new BigDecimal(jxcCustom.getAmount());
                    jxcCustom.setAmount(String.valueOf(oldCustomamount.subtract(diff)));
                    jxcCustomService.updateById(jxcCustom);

                    JxcCzrz jxcCzrz1 = new JxcCzrz();
                    jxcCzrz1.setId(DdygPiciController.setczrzid(id));
                    jxcCzrz1.setCompanyId(companyId);
                    jxcCzrz1.setInfor("交易方:"+traner+"金额+"+String.valueOf(diff));
                    jxcCzrz1.setType("客户修改");
                    R add1 = jxcCzrzService.add(jxcCzrz1);
                }
            }
        }else if (payamount.compareTo(oldpayamount) < 0){
            //差值
            BigDecimal diff = oldpayamount.subtract(payamount);
            if (orstate.equals("1")){
                JxcSupplier jxcSupplier = jxcSupplierService.getBy(traner, companyId);
                if (jxcSupplier != null){
                    BigDecimal oldSupplieramount = new BigDecimal(jxcSupplier.getAmount());
                    jxcSupplier.setAmount(String.valueOf(oldSupplieramount.add(diff)));
                    jxcSupplierService.updateById(jxcSupplier);

                    JxcCzrz jxcCzrz1 = new JxcCzrz();
                    jxcCzrz1.setId(DdygPiciController.setczrzid(id));
                    jxcCzrz1.setCompanyId(companyId);
                    jxcCzrz1.setInfor("交易方:"+traner+"金额-"+String.valueOf(diff));
                    jxcCzrz1.setType("供应商修改");
                    R add1 = jxcCzrzService.add(jxcCzrz1);
                }
            }else {
                JxcCustom jxcCustom = jxcCustomService.getBy(traner, companyId);
                if (jxcCustom != null){
                    BigDecimal oldCustomamount = new BigDecimal(jxcCustom.getAmount());
                    jxcCustom.setAmount(String.valueOf(oldCustomamount.add(diff)));
                    jxcCustomService.updateById(jxcCustom);

                    JxcCzrz jxcCzrz1 = new JxcCzrz();
                    jxcCzrz1.setId(DdygPiciController.setczrzid(id));
                    jxcCzrz1.setCompanyId(companyId);
                    jxcCzrz1.setInfor("交易方:"+traner+"金额-"+String.valueOf(diff));
                    jxcCzrz1.setType("客户修改");
                    R add1 = jxcCzrzService.add(jxcCzrz1);
                }
            }
        }

        return new R().ok(jxcPayinforService.updateById(jxcPayinfor));
    }
    @DeleteMapping
    public R delete(@RequestBody JSONObject params) {
        JSONArray idList = params.getJSONArray("idList");

        //去除空格

        logger.info("付款详情删除数据"+params.toString());
        String id = idList.get(0).toString();
        JxcPayinfor jxcPayinfor = jxcPayinforService.getById(id);
        String orstate = jxcPayinfor.getOrstate();
        String companyId = jxcPayinfor.getOtherid();
        BigDecimal payamount = new BigDecimal(jxcPayinfor.getPayamount());
        String traner = jxcPayinfor.getOther().trim();

        JxcCzrz jxcCzrz = new JxcCzrz();
        jxcCzrz.setId(DdygPiciController.setczrzid(id));
        jxcCzrz.setCompanyId(companyId);
        jxcCzrz.setInfor("交易方:"+traner+"金额:"+payamount);

        if (orstate.equals("1")){
            jxcCzrz.setType("供应商付款明细删除");
            R add = jxcCzrzService.add(jxcCzrz);
            //供应商
            JxcSupplier jxcSupplier = jxcSupplierService.getBy(traner, companyId);
            if (jxcSupplier != null){
                BigDecimal oldSupplieramount = new BigDecimal(jxcSupplier.getAmount());
                jxcSupplier.setAmount(String.valueOf(oldSupplieramount.add(payamount)));
                jxcSupplierService.updateById(jxcSupplier);

                JxcCzrz jxcCzrz1 = new JxcCzrz();
                jxcCzrz1.setId(DdygPiciController.setczrzid(id));
                jxcCzrz1.setCompanyId(companyId);
                jxcCzrz1.setInfor("交易方:"+traner+"金额:+"+jxcPayinfor.getPayamount());
                jxcCzrz1.setType("供应商修改");
                R add1 = jxcCzrzService.add(jxcCzrz1);
            }
        }else {
            jxcCzrz.setType("客户付款明细删除");
            R add = jxcCzrzService.add(jxcCzrz);
            JxcCustom jxcCustom = jxcCustomService.getBy(traner, companyId);
            if (jxcCustom != null){
                BigDecimal oldCustomamount = new BigDecimal(jxcCustom.getAmount());
                jxcCustom.setAmount(String.valueOf(oldCustomamount.add(payamount)));
                jxcCustomService.updateById(jxcCustom);

                JxcCzrz jxcCzrz1 = new JxcCzrz();
                jxcCzrz1.setId(DdygPiciController.setczrzid(id));
                jxcCzrz1.setCompanyId(companyId);
                jxcCzrz1.setInfor("交易方:"+traner+"金额:+"+jxcPayinfor.getPayamount());
                jxcCzrz1.setType("客户修改");
                R add1 = jxcCzrzService.add(jxcCzrz1);
            }
        }
        jxcPayinforService.removeByIds(idList);
        return new R().ok("删除成功");
    }
    public String generateRandomString() {
        String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        StringBuilder sb = new StringBuilder(8);
        for (int i = 0; i < 8; i++) {
            int index = (int) (Math.random() * characters.length());
            sb.append(characters.charAt(index));
        }
        return sb.toString();
    }
}
